15 ROS2- Action

  • In the new terminal and run:
cs
ros2 pkg create action_tutorials_interfaces
cd action_tutorials_interfaces
 mkdir action
cd action
  • In the same terminal and run:
code Fibonacci.action
  • Add these lines, save and close it:
int32 order
---
int32[] sequence
---
int32[] partial_sequence

Pasted image 20251012155854.png

  • In the same terminal and run:
cd ..
code CMakeLists.txt
  • You must add these lines  before the line “if(BUILD_TESTING)” ,save and close it:
find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Fibonacci.action"
)

Pasted image 20251012155938.png

  • In the same terminal and run:
code package.xml
  • within the <package> element of package.xml ,save and close it:

<buildtool_depend>rosidl_default_generators</buildtool_depend>
<depend>action_msgs</depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<small> Reference : https://docs.ros.org/en/humble/Installation.html </small>

Pasted image 20251012160025.png

•In the same terminal and run:

cb
sb
  • Open another terminal and run:
ros2 interface show action_tutorials_interfaces/action/Fibonacci

Pasted image 20251012160101.png

Pasted image 20251012160110.png

Writing an action server

  • In a terminal and run:
cd ~/
mkdir testAction
cd testAction
code fibonacci_action_server.py

•Copy and paste the code into it, save and close it.

import time

import rclpy
from rclpy.action import ActionServer
from rclpy.node import Node

from action_tutorials_interfaces.action import Fibonacci


class FibonacciActionServer(Node):

    def __init__(self):
        super().__init__('fibonacci_action_server')
        self._action_server = ActionServer(
            self,
            Fibonacci,
            'fibonacci',
            self.execute_callback)

    def execute_callback(self, goal_handle):
        self.get_logger().info('Executing goal...')

        feedback_msg = Fibonacci.Feedback()
        feedback_msg.partial_sequence = [0]

        for i in range(1, goal_handle.request.order):
            feedback_msg.partial_sequence.append(i)
            self.get_logger().info('Feedback: {0}'.format(feedback_msg.partial_sequence))
            goal_handle.publish_feedback(feedback_msg)
            time.sleep(1)

        goal_handle.succeed()

        result = Fibonacci.Result()
        result.sequence = feedback_msg.partial_sequence
        return result


def main(args=None):
    rclpy.init(args=args)

    fibonacci_action_server = FibonacciActionServer()

    rclpy.spin(fibonacci_action_server)


if __name__ == '__main__':
    main()
  • In the same terminal and run:
sudo chmod +x fibonacci_action_server.py
python3 fibonacci_action_server.py
  • Open another terminal and run:
ros2 action send_goal fibonacci action_tutorials_interfaces/action/Fibonacci "{order: 5}"
  • In a terminal and run:
cd ~/
mkdir testAction
cd testAction
code fibonacci_action_client.py
import rclpy
from rclpy.action import ActionClient
from rclpy.node import Node

from action_tutorials_interfaces.action import Fibonacci


class FibonacciActionClient(Node):

    def __init__(self):
        super().__init__('fibonacci_action_client')
        self._action_client = ActionClient(self, Fibonacci, 'fibonacci')

    def send_goal(self, order):
        goal_msg = Fibonacci.Goal()
        goal_msg.order = order

        self._action_client.wait_for_server()

        self._send_goal_future = self._action_client.send_goal_async(goal_msg, feedback_callback=self.feedback_callback)

        self._send_goal_future.add_done_callback(self.goal_response_callback)

    def goal_response_callback(self, future):
        goal_handle = future.result()
        if not goal_handle.accepted:
            self.get_logger().info('Goal rejected :(')
            return

        self.get_logger().info('Goal accepted :)')

        self._get_result_future = goal_handle.get_result_async()
        self._get_result_future.add_done_callback(self.get_result_callback)

    def get_result_callback(self, future):
        result = future.result().result
        self.get_logger().info('Result: {0}'.format(result.sequence))
        rclpy.shutdown()

    def feedback_callback(self, feedback_msg):
        feedback = feedback_msg.feedback
        self.get_logger().info('Received feedback: {0}'.format(feedback.partial_sequence))


def main(args=None):
    rclpy.init(args=args)

    action_client = FibonacciActionClient()

    action_client.send_goal(10)

    rclpy.spin(action_client)


if __name__ == '__main__':
    main()
  • In the same terminal and run:
sudo chmod +x fibonacci_action_client.py
  • Open another terminal and run:
python3 fibonacci_action_client.py
  • make sure "fibonacci_action_server.py" still running